<?
require_once '../../modelo/config.php';



class mUser{
	public $id;
    public $nombre;
    public $apellido1;
	public $apellido2;
	public $rol;
	public $email;
	
 
    protected $conx;

    public function __construct(){
        $this->nombre = "";
        $this->apellido1 = "";
		$this->apellido2 = "";
 		$this->rol = "";
		$this->email = "";
		
		$this->conx = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    }
 
    public function get($id){
        $id = htmlspecialchars($id);
 
        $query = "SELECT NOMBRE, APELLIDO1, APELLIDO2, ROL, EMAIL
		FROM user WHERE id = " . $id . " LIMIT 0,1";
        $resultSet = $this->conx->query($query);
		
		
		
 
        if($resultSet){
            if($row = $resultSet->fetch_object()){
                $this->nombre = $row->NOMBRE;
                $this->apellido1 = $row->APELLIDO1;
				$this->apellido2 = $row->APELLIDO2;
				$this->rol = $row->ROL;
				$this->email = $row->EMAIL;
				
 
                return true;
			}
		}
    }
 
    public function set(){
        $_nombres = htmlspecialchars($this->nombres);
        $_apellido1s = htmlspecialchars($this->apellido1s);
		$_apellido2s = htmlspecialchars($this->apellido2s);
		$_rols = htmlspecialchars($this->rols);
		$_emails = htmlspecialchars($this->emails);
 
        $query = "INSERT INTO user(NOMBRE, APELLIDO1, APELLIDO2, ROL, EMAIL, TIPO, CONTRASENA)
                VALUES(     '" . $_nombres . "',
							'" . $_apellido1s . "',
							'" . $_apellido2s . "',
							'" . $_rols . "',
							'" . $_emails . "',
							'U',
							'" . $_emails . "')";
        $this->conx->query($query);

        if($this->conx->affected_rows > 0)
            return true;
        else
            return false;
    }
 
    public function update($id){
        $id = htmlspecialchars($id);
        $_nombres = htmlspecialchars($this->nombres);
        $_apellido1s = htmlspecialchars($this->apellido1s);
		$_apellido2s = htmlspecialchars($this->apellido2s);
		$_rols = htmlspecialchars($this->rols);
		$_emails = htmlspecialchars($this->emails);
 
        $query = "UPDATE user
            SET      NOMBRE = '" . $_nombres . "'
                    ,APELLIDO1 = '" . $_apellido1s . "'
					,APELLIDO2 = '" . $_apellido2s . "'
					,ROL = '" . $_rols . "'
					,EMAIL = '" . $_emails . "'
			
            WHERE ID = " . $id;
        $this->conx->query($query);
 
        if($this->conx->affected_rows > 0)
            return true;
        else
            return false;
    }
 
    public function delete($id){
        $id = htmlspecialchars($id);
 
        $query = "DELETE FROM user WHERE ID = " . $id;
        $this->conx->query($query);
 
        if($this->conx->affected_rows > 0)
            return true;
        else 
            return false;
    }
 
    public function getAll(){
        $rows = array();
 
        $query = "SELECT * FROM user";
        $resultSet = $this->conx->query($query);
 
        if($resultSet)
            while($row = $resultSet->fetch_object())
                $rows[] = $row;

        //$resultSet->close();
 
        return $rows;
    }
 
    public function __destruct(){
        $this->conx->close();
    }
	
}	
?>
